package com.github.winyiwin;

import java.util.Map;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class KobeRejectedPolicy implements RejectedExecutionHandler {


    private String threadPoolName;

    private Map<String, Long> rejectMap;

    public KobeRejectedPolicy(String threadPoolName, Map<String, Long> rejectMap) {
        this.threadPoolName = threadPoolName;
        this.rejectMap = rejectMap;
    }

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        rejectMap.compute(threadPoolName, (k, v) -> (v == null) ? 1L : ++v);
        throw new RejectedExecutionException("Task " + r.toString() +
                " rejected from " + e.toString());
    }
}
